package com.cnhis.cloudhealth.clinical.hosnurse.hosadvice.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.cnhis.cloudhealth.commons.newid.GetNewId;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hosadvice.dao.HosAdviceDao;
import com.cnhis.cloudhealth.clinical.hosnurse.hosdoctoradvice.dao.HosDoctorAdviceDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

@Service
public class HosAdviceBo {
	 @Resource 
	  private HosAdviceDao hosAdviceDao;
	 @Resource
	 private HosDoctorAdviceDao hosDoctorAdviceDao;
	 @Resource
	 private GetNewId getNewId;
	 public ModelVo adviceList(Map map, int currPage,int pageSize)throws Exception{
			ModelVo mo = new ModelVo();
			Page pg = null;
			Integer adint = 0;
			try {
				Map m = hosDoctorAdviceDao.getlParaV_lPara262();
				map.put("adate", DateUtils.getDateString(new Date()));
				map.putAll(m);
				adint = hosAdviceDao.adviceint(map);
				pg = hosAdviceDao.adviceList(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}

			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(pg.getTotalNumber());
			mo.setUserdata("{totalsize:"+pg.getTotalNumber()+"}");
			mo.setRows(pg.getList());
			mo.setPage(currPage);
			mo.setRecords(adint);
			mo.setTotalPage(pg.getTotalPage());
			return mo;    		 
		 
		 
	 }
	private ModelVo packModelVo(String nid,Map map){
		ModelVo mo = new ModelVo();
		List<Map<String,String>> list = null;
		try {
			list = hosDoctorAdviceDao.queryForList(nid,map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list!=null&&list.size()>0) {

			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(list.size());
		}
		return mo;
	}

	 public ModelVo patientList(long bck01c)throws Exception{
			ModelVo mo = new ModelVo();
			List list = null;
			try {    
				    Map map = new HashMap();
				    map.put("bck01c", bck01c);
					list = hosAdviceDao.patientList(map);
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
	 }
	 public ModelVo stopAdviceList(Map map, int currPage,
			 int pageSize)throws Exception{
		 ModelVo mo = new ModelVo();
			Page pg = null;
			try {
				Map m = hosDoctorAdviceDao.getlParaV_lPara262();
				map.put("adate", DateUtils.getDateString(new Date()));
				map.putAll(m);
				pg = hosAdviceDao.stopAdviceList(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}

			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(pg.getTotalNumber());
			mo.setRows(pg.getList());
			return mo;
	 }
	 public ModelVo sendAdviceList(Map map, int currPage,
			 int pageSize)throws Exception{
		 ModelVo mo = new ModelVo();
			Page pg = null;
			try {
				Map m = hosDoctorAdviceDao.getlParaV_lPara262();
				map.put("adate", DateUtils.getDateString(new Date()));
				map.putAll(m);
				pg = hosAdviceDao.sendAdviceList(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}

			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(pg.getTotalNumber());
			mo.setRows(pg.getList());
			return mo;
	 }
	     public ModelVo sendOtherAdviceList(Map map, int currPage,
			 int pageSize)throws Exception{
		 ModelVo mo = new ModelVo();
			Page pg = null;
			try {
				Map m = hosDoctorAdviceDao.getlParaV_lPara262();
				map.put("adate", DateUtils.getDateString(new Date()));
				map.putAll(m);
				pg = hosAdviceDao.sendOtherAdviceList(map, currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}

			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(pg.getTotalNumber());
			mo.setRows(pg.getList());
			return mo;
	 }

	//是否皮试药物
	public ModelVo getPishiOrder(Map map){return packModelVo("HosAdvice.1643",map);}

	//插入过敏记录
	private void insertVAL(Map<String,Object> map)throws Exception{
		Integer cn1 = (Integer)hosAdviceDao.queryForExtObject("HosAdvice.pishi_002", map);	//获取病人过敏记录
		if(cn1==0){
			Long lVAN01 = getNewId.nextId();
			map.put("lVAN01",lVAN01);
			hosAdviceDao.insert("HosAdvice.pishi_003", map);	//插入过敏记录
		}else{
			hosAdviceDao.update("HosAdvice.pishi_004", map);	//更新过敏记录
		}
	}
	//登记皮试结果Clinic_Test_VAF25
	public ModelVo recordPishi(Map map)throws Exception{
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
//		Long lVAF01 = MapUtils.getLong(map,"lVAF01"); //医嘱ID
//		Integer lACF01 = MapUtils.getInteger(map,"lACF01");
		String lVAN08 = MapUtils.getString(map,"lVAN08");
//		String lBCE03 = MapUtils.getString(map,"lBCE03");
//		String BCE03C = MapUtils.getString(map,"BCE03C");
//		String BCE01B = MapUtils.getString(map,"BCE01B");
//		String VAN15 = MapUtils.getString(map,"VAN15");
//		String van09 = MapUtils.getString(map,"van09");
		Integer lmode = MapUtils.getInteger(map,"lmode",0);
		String VAN08b = "";

		Map<String,Object> rMap1 = (Map<String,Object>)hosAdviceDao.queryForExtObject("HosAdvice.pishi_001", map);	//获取医嘱内容
		Long lVAA01 = MapUtils.getLong(rMap1,"vaa01");
		Long lVAA07 = MapUtils.getLong(rMap1,"vaf06");
		Long lBBX01 = MapUtils.getLong(rMap1,"bbx01");
		String lBBX05 = MapUtils.getString(rMap1,"bbx05");
		Long lVAF07 = MapUtils.getLong(rMap1,"vaf07");
		String lBDA01 = MapUtils.getString(rMap1,"bda01");
		Long lVAF01A = MapUtils.getLong(rMap1,"vaf01a");
		Long lCBM01 = MapUtils.getLong(rMap1,"cbm01");
		String mDate = MapUtils.getString(rMap1,"cbm16");

		map.put("lACF01",2);
		map.put("lVAA01",lVAA01);
		map.put("lVAA07",lVAA07);
		map.put("lBBX01",lBBX01);
		map.put("lBBX05",lBBX05);
		map.put("lVAF07",lVAF07);
		map.put("lBDA01",lBDA01);
		map.put("lVAF01A",lVAF01A);
		map.put("lCBM01",lCBM01);
		map.put("mDate",mDate);
		map.put("lVAN08",lVAN08);
		if(lmode==3){
			insertVAL(map);
		}

		if("1".equals(lVAN08)){
			VAN08b = "+";
		}else{
			VAN08b = "-";
			lVAN08 = "0";
		}
		map.put("VAN08b",VAN08b);
		map.put("lVAN08",lVAN08);
		if("1".equals(lBDA01)||"2".equals(lBDA01)){
			hosAdviceDao.update("HosAdvice.pishi_005", map);	//更新医嘱皮试嘱托
			insertVAL(map);
			Integer cn2 = (Integer)hosAdviceDao.queryForExtObject("HosAdvice.pishi_006", map);	//获取病人过敏药物
			if("1".equals(lVAN08)&&cn2==0){
				Long lVAL01 = getNewId.nextId();
				map.put("lVAL01",lVAL01);
				hosAdviceDao.insert("HosAdvice.pishi_007", map);	//插入过敏药物
			}
		}else{
			List<Map<String,Object>> rList1 = hosAdviceDao.queryForList("HosAdvice.pishi_008", map);	//获取诊疗药品用法用量
			if(rList1 == null || rList1.size()==0){
				return new ModelVo(StaticKeys.OH_FALSE, "过敏药物错误，请先在药品目录维护中添加此过敏皮试项目对应的药品！");
			}
			hosAdviceDao.update("HosAdvice.pishi_009", map);	//更新医嘱皮试嘱托
			insertVAL(map);
			if("1".equals(lVAN08)) {
				Integer cn4 = (Integer)hosAdviceDao.queryForExtObject("HosAdvice.pishi_006", map);	//获取病人过敏药物
				if(cn4==0) {
					for (Map<String, Object> obj : rList1) {
						lBBX01 = MapUtils.getLong(obj,"bbx01a",0l);
						lBBX05 = MapUtils.getString(obj,"bbx05","");
						map.put("lBBX01",lBBX01);
						map.put("lBBX05",lBBX05);
						Long lVAL01 = getNewId.nextId();
						map.put("lVAL01",lVAL01);
						hosAdviceDao.insert("HosAdvice.pishi_007", map);	//插入过敏药物
					}
				}
			}
		}


		return mo;
	}
}
